package dao.impl;

import dao.UserWordDao;
import entity.UserWord;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.DruidUtils;

import java.sql.Connection;
import java.util.List;

public class UserWordDaoImpl implements UserWordDao {
    @Override
    public int recordSound(UserWord userWord) {
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            //AI编写
            String sql = "insert into user_word ( time, record, user_id) VALUES (?,?,?)";
            //有insert方法可以使用，只不过没有使用
            int update = queryRunner.update(connection, sql, userWord.getTime(), userWord.getRecord(), userWord.getUserId());
            connection.close();
            return update;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<UserWord> getMonthRecordByUserId(Integer userId) {
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());
            String sql = "SELECT *  FROM user_word\n" +
                    "                       WHERE YEAR(record) = YEAR(CURRENT_DATE())\n" +
                    "                       AND MONTH(record) = MONTH(CURRENT_DATE()) and user_id=?";
            List<UserWord> query = queryRunner.query(connection, sql,
                    new BeanListHandler<>(UserWord.class, basicRowProcessor), userId);
            //有insert方法可以使用，只不过没有使用
            connection.close();
            return query;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
